package com.epfresh.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.bean.LogisticsInfoEntity;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderLogisticsInfoActivity extends BaseActivity {

	static final String TAG = "OrderLogisticsInfoActivity";
	String logisticsNo,phone;

	TextView tvPoster,tvNumber,tvTime,tvUrge;
	TextView tvReceiver,tvReceiverPhone,tvAddress,tvPayType,tvDistance;
	ListView lv;
	ArrayList<LogisticsInfoEntity.ListBean> list = new ArrayList<>();
	LogisticsAdapter logisticsAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		setToolbarTitle("订单跟踪");
		logisticsNo = getIntent().getStringExtra("logisticsNo");
		if(logisticsNo==null) {
			T.toast("数据有误，请重试");
			finish();
			return;
		}
		mHttpInfo();
	}
	private void initView() {
		setContentView(R.layout.activity_order_logisticsinfo);
		tvPoster = (TextView)findViewById(R.id.tv_poster);
		tvNumber = (TextView)findViewById(R.id.tv_number);
		tvDistance = (TextView)findViewById(R.id.tv_distance);
		tvTime = (TextView)findViewById(R.id.tv_time);
		tvReceiver = (TextView)findViewById(R.id.tv_receiver);
		tvReceiverPhone = (TextView)findViewById(R.id.tv_receiver_phone);
		tvReceiverPhone.setOnClickListener(this);
		tvAddress = (TextView)findViewById(R.id.tv_address);
		tvPayType = (TextView)findViewById(R.id.tv_pay_type);

		lv = (ListView)findViewById(R.id.lv);
		logisticsAdapter = new LogisticsAdapter(this,list,R.layout.item_money_and_logistics);
		lv.setAdapter(logisticsAdapter);
		lv.setDivider(null);
		baseback.setVisibility(View.VISIBLE);
		baseback.setOnClickListener(this);
		tvUrge = (TextView)findViewById(R.id.tv_urge);
		tvUrge.setOnClickListener(this);
	}
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.baseback:
				// 返回上一页
				finish();
				break;

			case R.id.tv_urge:
				Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));//调出来拨打页面；
				startActivity(call);
				break;
			case R.id.tv_receiver_phone:
				Intent phone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvReceiverPhone.getText().toString()));//调出来拨打页面；
				startActivity(phone);
			default:
				break;
		}
	}
	class LogisticsAdapter extends CommonAdapter<LogisticsInfoEntity.ListBean> {
		public LogisticsAdapter(Context context, List<LogisticsInfoEntity.ListBean> historyList, int a) {
			super(context, historyList, a);
		}

		@Override
		public void convert(ViewHolder holder, final LogisticsInfoEntity.ListBean bean) {
			holder.setText(R.id.tv_content, bean.getMerInfo());
			holder.setText(R.id.tv_time, bean.getNodeTime());
			holder.getView(R.id.view_2).setVisibility(View.VISIBLE);
			holder.getView(R.id.view_1).setVisibility(View.VISIBLE);
			holder.getView(R.id.v_line).setVisibility(View.VISIBLE);
			if(list.indexOf(bean)==list.size()-1){
				holder.getView(R.id.view_2).setVisibility(View.INVISIBLE);
				holder.getView(R.id.view_1).setVisibility(View.VISIBLE);
				holder.getView(R.id.v_line).setVisibility(View.INVISIBLE);
			}
			if(list.indexOf(bean)==0){
				holder.getView(R.id.view_2).setVisibility(View.VISIBLE);
				holder.getView(R.id.view_1).setVisibility(View.INVISIBLE);
				((ImageView)holder.getView(R.id.iv_state)).setImageResource(R.mipmap.order_red_dot);
			}
			if(list.size()==1){
				holder.getView(R.id.view_2).setVisibility(View.INVISIBLE);
				holder.getView(R.id.view_1).setVisibility(View.INVISIBLE);
				((ImageView)holder.getView(R.id.iv_state)).setImageResource(R.mipmap.order_red_dot);
			}
		}
	}
	/** 查看物流  */
	private void mHttpInfo() {
		RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
		data.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_order_order_track);
		Map<String, String> map = new HashMap<>();
		map.put("orderId", logisticsNo);
		data.setParameters(map);
		request(data, com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_order_order_track, new OnRequestListener<LogisticsInfoEntity>() {
			@Override
			public LogisticsInfoEntity jsonToObj(String responseStr) {
				return new Gson().fromJson(responseStr, LogisticsInfoEntity.class);
			}

			@Override
			public void onFail(int failCode, Object arg, Object tag) {
				hideProgressDialog();
				showExceptionView(R.mipmap.upload_error, "网络错误", HttpRequest.FAIL_NET_FAULT);
			}

			@Override
			public void onResponseError(int failCode, Object arg, Object tag) {
				//hideProgressDialog();
				showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
			}
			@Override
			public void onStart(Object tag) {
				showProgressDialog();
			}

			@Override
			public void onResponse(ResponseEntity<LogisticsInfoEntity> response, Object tag) {
				LogisticsInfoEntity responseElement = response.getResponseElement();
				updateOrderInfo(responseElement);
				hideProgressDialog();
			}

		});
	}
	void updateOrderInfo(LogisticsInfoEntity moneyPath){
		if(moneyPath.getIsReminder()==1){
			tvUrge.setVisibility(View.VISIBLE);
		}
		phone = moneyPath.getDriverPhone();
		if(moneyPath.getReceiver()==null||moneyPath.getReceiver().equals("")||moneyPath.getReceiver().equals("null")){
			moneyPath.setReceiver("无");
		}
		if(moneyPath.getReceiverPhone()==null||moneyPath.getReceiverPhone().equals("")||moneyPath.getReceiverPhone().equals("null")){
			moneyPath.setReceiverPhone("无");
		}
		if(moneyPath.getDistributionTitle()==null||moneyPath.getDistributionTitle().equals("")||moneyPath.getDistributionTitle().equals("null")){
			moneyPath.setDistributionTitle("无");
		}
		tvReceiverPhone.setText(moneyPath.getReceiverPhone());
		tvNumber.setText("订单："+moneyPath.getOrderNo());
		tvPoster.setText("配送方："+moneyPath.getDistributionTitle());
		tvPayType.setText("支付方式："+moneyPath.getPayTypeTitle());
		if(moneyPath.getDeliveryType()%2==1){//配送
			tvTime.setText("配送时间："+moneyPath.getDeliveryDateTime());
			tvReceiver.setText("收货人：" +moneyPath.getReceiver());
			tvAddress.setText("配送地址："+moneyPath.getReceiverAddress());
			tvDistance.setText("配送公里数：约"+moneyPath.getExpressDistance()+"公里");
			tvPoster.setText("配送方："+moneyPath.getDistributionTitle());

		}else {//自提
			tvTime.setText("提货时间："+moneyPath.getDeliveryDateTime());
			tvReceiver.setText("提货人：" +moneyPath.getReceiver());
			tvPoster.setText("自提店铺："+moneyPath.getDistributionTitle());
			tvDistance.setVisibility(View.GONE);
			tvAddress.setVisibility(View.GONE);
		}
		list.addAll(moneyPath.getList());
		logisticsAdapter.notifyDataSetChanged();
	}
}
